/*
Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn't matter what you leave beyond the new length.
*/
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
	int removeDuplicates(vector<int>& nums) {
		int start = 0, count = 0;

		if (nums.size() == 0)
		{
			return 0;
		}

		int elements = nums[0];
		count = 1;

		for (int i = 0; i < nums.size(); i++)
		{
			if (nums[i] != elements)
			{
				nums[start++] = elements;
				elements = nums[i];
				count = 1;
			}
			if (count == 2)
			{
				nums[start++] = elements;
			}

			count++;
		}
		nums[start++] = elements;
		return start++;

	}


};

int main()
{
	cout << "Remove Duplicates from Sorted Array:  " << endl;

	int length;
	cout << "Input the length of the nums: "<< endl;
	
	cin >> length;

	vector<int> nums;

	while (length--)
	{
		int element;
		cin >> element;
		nums.push_back(element);
	}

	Solution s;

	cout << "The result: "<< s.removeDuplicates(nums) << endl;

	system("pause");

	return 0;
}